<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class BaseDB extends DB
{
    /**
     * 基本DB
     */

    public function __construct()
    {
    }

    /**
     * 获取接口权限
     */
    public function getAccessLast()
    {
        $table = 'access';
        $start = strtotime(date('Ymd',time()).'000001');
        $end = strtotime(date('Ymd',time()).'235959');
        $model = DB::table($table)
            ->where('created_at','>=',$start)
            ->where('created_at','<=',$end)
            ->orderBy('id','desc')
            ->first();
        if ($model) { return $model->access; }
    }

    /**
     * 设置接口权限
     */
    public function setAccess()
    {
        $table = 'access';
        $access = $this->getRandStr(rand(60,80));
        $model = DB::table($table)
            ->where('access',$access)
            ->orderBy('id','desc')
            ->first();
        if ($model) { $access = $this->getRandStr(rand(60,80)); }
        $t = time();
        $createTime = date('Y-m-d H:i:s',$t);
        $addArr = [
            'access' => $access,
            'created_at' => $t,
            'createTime' => $createTime,
        ];
        DB::table($table)->insertGetId($addArr);
        return $access;
    }

//    /**
//     * 获取接口权限
//     */
//    public function getAccess()
//    {
//        $table = 'access';
//        $start = strtotime(date('Ymd',time()).'000001');
//        $end = strtotime(date('Ymd',time()).'235959');
//        $model = DB::table($table)
//            ->where('created_at','>=',$start)
//            ->where('created_at','<=',$end)
//            ->orderBy('id','desc')
//            ->first();
//        if ($model) { return $model->access; }
//        $access = $this->getRandStr(rand(60,80));
//        $model2 = DB::table($table)
//            ->where('access',$access)
//            ->orderBy('id','desc')
//            ->first();
//        if ($model2) { $access = $this->getRandStr(rand(60,80)); }
//        $t = time();
//        $createTime = date('Y-m-d H:i:s',$t);
//        $addArr = [
//            'access' => $access,
//            'created_at' => $t,
//            'createTime' => $createTime,
//        ];
//        $lastId = DB::table($table)->insertGetId($addArr);
//        return $access;
//    }

    /**
     * 生成随机字符串，数字字母字符混合
     */
    public function getRandStr($length=6)
    {
        //数字、字母组合
        $str = '';
        $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz!:=-_!$%^&*';
        $max = strlen($chars) - 1;
        for($i = 0; $i < $length; $i++) {
            $str .= $chars[mt_rand(0, $max)];
        }
        return $str;
    }

    /**
     * 生成随机验证码
     */
    public function getRandYzm($length=6)
    {
        //数字、字母组合
        $str = '';
        $chars = '1234567890';
        $max = strlen($chars) - 1;
        for($i = 0; $i < $length; $i++) {
            $str .= $chars[mt_rand(0, $max)];
        }
        return $str;
    }

    /**
     * JSON转化为数组
     */
    public function jsonToArr($str) {
        if (is_string($str))
            $str = json_decode($str);
        $arr = array();
        foreach($str as $k=>$v) {
            if(is_object($v) || is_array($v))
                $arr[$k] = $this->jsonToArr($v);
            else
                $arr[$k] = $v;
        }
        return $arr;
    }

    /**
     * 通过UID，获取用户名称
     */
    public function getUnameByUid($uid)
    {
        $table = 'user';
        $model = DB::table($table)->where('id',$uid)->first();
        return $model ? $model->name : '';
    }
}